
import Sortable from "sortablejs";
export const OTMixinSortable = {
    methods: {
        updateSort() {
            if (!this.table.sortable) {
                return
            }
            this.$nextTick(() => {
                //el绑定元素需要找到vxe的DOM下的tbody元素
                Sortable.create(this.$table.$el.querySelector('.body--wrapper>.vxe-table--header .vxe-header--row'),
                    {
                        filter: '.col--fixed',
                        handle: '.vxe-header--column',
                        onEnd: ({newIndex, oldIndex}) => {
                            console.log(newIndex, oldIndex);
                            let tableColumn = JSON.parse(JSON.stringify(this.$table.getColumns()));
                            let newColumn = tableColumn[newIndex];
                            let oldColumn = tableColumn[oldIndex];
                            for (let i = 0; i < this.table.columns.length; i++) {
                                let column = this.table.columns[i];
                                if (oldColumn.field == column.field) {
                                    oldIndex = i;
                                } else if (newColumn.field == column.field) {
                                    newIndex = i;
                                }
                            }
                            let columns = this.table.columns;

                            this.$table.loadColumn([]);
                            let spliceElement = columns.splice(oldIndex, 1)[0];
                            columns.splice(newIndex, 0, spliceElement);
                            this.table.columns = columns;
                            this.$nextTick(() => {
                                this.$table.loadColumn(this.table.columns);
                            })

                        }
                    })
            })
        },
    }
}
